/**
 * Created with IntelliJ IDEA.
 * User: liusiyang
 * Date: 13-11-27
 * Time: 13:29
 * To change this template use File | Settings | File Templates.
 */

var svg;

var _width = 0;
var _height = 0;

var first = true;
var offsetYForOld = -20;

function onReal(msg) {

    msg = eval(msg);
    for (var i = 0; i < msg.length; i++) {
        refresh(msg[i].k, msg[i].v, msg[i].s);
    }
    //5秒再次 调用 runLoadSvgJavaScript()
    setTimeout("runLoadSvgJavaScript()",5000);
}

var _bodyWidth = $(window).width();
var _bodyHeight = $(window).height();
var _oldVersion = false;
//初始化调用加载mvg文件
function init() {
    svg = SVG('HxView');
    $("#HxView").attr("width", _bodyWidth);
    $("#HxView").attr("height", _bodyHeight);
    runLoadSvgJavaScript();
}

function runLoadSvgJavaScript(){
 window.loadSvg.runLoadSvgJavaScript();
}
var Status = {
    stop: "0",
    run: "1",
    fault: "2"
}
var Color = {
    stop: "red",
    run: "green",
    fault: "yellow"
}

var _pop = {
    type: 'pop',
    dur: 500,
    cycle: 800
}
var pipe = {
    type: 'pipe',
    direction: {
        left: 'left',
        right: 'right'
    }
}
var _clickKey = '';
function refresh(key, value, state) {
    _clickKey = key;
    var _keys = key.split('_');
    if (_keys.length <= 1) {
        if (key === 'time') {
            AtomJS.setValue("timeMark", value);
            if (SVG.get("timeMark") == undefined) {
                var text = svg.text(value);
                text.attr("id", "timeMark");
                text.x(5);
                text.y(0);
                text.font({
//                    family: 'Helvetica', size: 18, leading: '1.4em', fill: '#eee'
                    family: 'Helvetica', size: 18, leading: '0.8em', fill: '#eee'
                });
            } else {

                SVG.get("timeMark").text(value);
            }
        } else if (key === 'status') {
//            AtomJS.setValue("26_Text_01", value);
        }else if (key === 'orgName') {
             alert("orgName"+value);
            SVG.get("stationName").text(value);
        }
    } else {
        var _mark = (key.split("_"))[0];
        switch (_mark) {
            case"Text":
                var keyDom = document.getElementById(key);
                var keyDom1 = SVG.get(key);
                if (keyDom != null && keyDom.nodeName == "text") {

                    AtomJS.setValue(key, value);
                    $("tspan").attr("dy",0);
                    return;
                    var keyChild = keyDom.firstChild;
                    if (keyChild.nodeName != "text")return;
                    key = keyChild.id;
                } else if((keyDom != null && keyDom.nodeName == "g")) {

                    if (keyDom1.node.firstElementChild.nodeName!="text")return;
                    key=keyDom1.node.firstElementChild.id;

                }else {
                    return;
                }
                var color = "#25FF03";
                var svgValue = value;
                AtomJS.shine(key, true);
                switch (state) {
                    case 1:
                        svgValue = value + "↑";
                        color = '#FF8000';
                        break;
                    case -1:
                        svgValue = value + "↓";
                        color = '#FF8000';
                        break;
                    case 2:
                        svgValue = value + "↑";
                        color = '#FF0000';
                        break;
                    case -2:
                        svgValue = value + "↓";
                        color = '#FF0000';
                        break;
                    default :
                        AtomJS.shine(key, false);
                }
                if (value == undefined)break;
                AtomJS.setValue(key, svgValue);
                AtomJS.setStyle(key, {fill: color, stroke: color});
                if((keyDom != null && keyDom.nodeName == "g")){
                    keyDom1.node.firstElementChild.firstChild.attributes.dy.nodeValue='0';
                }
                return;
            case"Shine":
                AtomJS.shine(key, value == Status.run);
                return;
            case"Line":
                AtomJS.shine(key, value == Status.run);
                return;
            case"Fa":
                AtomJS.shine(key, value == Status.run);
                if (value == Status.run) {
                    AtomJS.setStyle(key, {fill: Color.run, stroke: Color.run});
                } else if (value == Status.stop) {
                    AtomJS.setStyle(key, {fill: Color.stop, stroke: Color.stop});
                } else if (value == Status.fault) {
                    AtomJS.setStyle(key, {fill: Color.fault, stroke: Color.fault});
                } else {
                    AtomJS.setStyle(key);
                }
                return;
            case"WaterW":
                return;
            case"AnimateC":
                if (value == Status.run) {
                    AtomJS.addAnimate(key, 'rotate', 1000, 2, 100);
                } else {
                    AtomJS.delAnimate(key);
                }
                return;
            case"Pop":
                var _popEl = SVG.get(key);
                var _pops = [];
                var m = 0;
                for (var i = 0; i < _popEl.node.childNodes.length; i++) {
                    if (_popEl.node.childNodes[i].nodeName == 'g') {
                        _pops[m] = _popEl.node.childNodes[i].id;
                        m++;
                    }
                }
                /*AtomJS.addAnimate(_pops, _pop);
                 var _popEl = SVG.get(key);
                 var _pops = [];
                 for (var i = 0; i < _popEl.node.childNodes.length; i++) {
                 _pops[i] = _popEl.node.childNodes[i].instance.attr("id");
                 }*/
                if (value == Status.run) {
                    AtomJS.addAnimate(_pops, _pop);
                } else {
                    AtomJS.delAnimate(_pops[0]);
                }
                return;
            case"PipeR":
                if (value == Status.run) {
                    AtomJS.addAnimate(key, pipe, pipe.direction.right);
                    AtomJS.delAnimate(key, pipe.type);
                    AtomJS.addAnimate(key, pipe, pipe.direction.right);
                }else if(value == Status.fault){
                    AtomJS.addAnimate(key, pipe, pipe.direction.left);
                    AtomJS.delAnimate(key, pipe.type);
                    AtomJS.addAnimate(key, pipe, pipe.direction.left);
                } else {
                    AtomJS.addAnimate(key, pipe, pipe.direction.right);
                    AtomJS.delAnimate(key, pipe.type);
                }
                return;
            case"PipeL":
                if (value == Status.run) {
                    AtomJS.addAnimate(key, pipe, pipe.direction.left);
                    AtomJS.delAnimate(key, pipe.type);
                    AtomJS.addAnimate(key, pipe, pipe.direction.left);
                }else if(value == Status.fault){
                    AtomJS.addAnimate(key, pipe, pipe.direction.right);
                    AtomJS.delAnimate(key, pipe.type);
                    AtomJS.addAnimate(key, pipe, pipe.direction.right);
                } else {
                    AtomJS.addAnimate(key, pipe, pipe.direction.left);
                    AtomJS.delAnimate(key, pipe.type);
                }
                return;
            case"AnimateH":
                return;
            case"AnimateV":
                return;
            case"Circle":
                var _tmpEl = AtomJS.el(key);
                if (_tmpEl != null && _tmpEl.node.nodeName == "g") {
                    var keyChild = _tmpEl.node.firstChild;
                    if (keyChild.nodeName != "ellipse")return;
                    key = keyChild.id;
                }
                AtomJS.shineColor(key, value);
                return;
            case"Polygon":
                AtomJS.shineColor(key, value);
                return;
            case"Polyline":
                AtomJS.shineColor(key, value);
                return;
        }
        var _tmpEl = AtomJS.el(key);
        if (_tmpEl != undefined && _mark == 'Circle') {
            _tmpEl.attr("onclick", "dc('" + _clickKey + "',evt)");
            _tmpEl.style("cursor", "hand");
        }
    }
    svg.attr("onmousemove", "svgClick(evt)");
}
